package com.fau.ss.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fau.ss.entity.MedicalAdmin;
import com.fau.ss.entity.MedicalCallHealthSymptom;
import com.fau.ss.entity.MedicalMediaInfo;
import com.fau.ss.entity.MedicalSsPurchase;
import com.fau.ss.service.IIllnessStaService;
import com.fau.ss.util.DateUtil;

@Service
@Transactional(readOnly = true)
public class IllnessStaService extends BaseService implements IIllnessStaService {

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> countByAge(MedicalSsPurchase purchase, MedicalAdmin admin) {
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		List<MedicalSsPurchase> list1 = new ArrayList<MedicalSsPurchase>();
		List<MedicalSsPurchase> list2 = new ArrayList<MedicalSsPurchase>();
		List<MedicalSsPurchase> list3 = new ArrayList<MedicalSsPurchase>();
		List<MedicalSsPurchase> list4 = new ArrayList<MedicalSsPurchase>();
		List<MedicalSsPurchase> list5 = new ArrayList<MedicalSsPurchase>();
		StringBuilder hql = new StringBuilder("from MedicalSsPurchase a where 1=1 and a.hospitalId = ? and a.age between ? and ? ");
		if(StringUtils.isNotBlank(purchase.getIllness())){
			hql.append(" and a.illness = ? group by a.customerId ");
			list1 = this.iBaseDao.getBySql(hql.toString(), null, admin.getEnterpriseId(), 20, 30, purchase.getIllness());
			list2 = this.iBaseDao.getBySql(hql.toString(), null, admin.getEnterpriseId(), 31, 40, purchase.getIllness());
			list3 = this.iBaseDao.getBySql(hql.toString(), null, admin.getEnterpriseId(), 41, 50, purchase.getIllness());
			list4 = this.iBaseDao.getBySql(hql.toString(), null, admin.getEnterpriseId(), 51, 60, purchase.getIllness());
			list5 = this.iBaseDao.getBySql("from MedicalSsPurchase a where 1=1 and a.hospitalId = ? and a.age > ? and a.illness = ? group by a.customerId ", null, admin.getEnterpriseId(), 60, purchase.getIllness());
		}else{
			hql.append(" group by a.customerId ");
			list1 = this.iBaseDao.getBySql(hql.toString(), null, admin.getEnterpriseId(), 20, 30);
			list2 = this.iBaseDao.getBySql(hql.toString(), null, admin.getEnterpriseId(), 31, 40);
			list3 = this.iBaseDao.getBySql(hql.toString(), null, admin.getEnterpriseId(), 41, 50);
			list4 = this.iBaseDao.getBySql(hql.toString(), null, admin.getEnterpriseId(), 51, 60);
			list5 = this.iBaseDao.getBySql("from MedicalSsPurchase a where 1=1 and a.hospitalId = ? and a.age > ? group by a.customerId ", null, admin.getEnterpriseId(), 60);
		}
		map.put("20~30岁", list1.size());
		map.put("31~40岁", list2.size());
		map.put("41~50岁", list3.size());
		map.put("51~60岁", list4.size());
		map.put("60岁以上", list5.size());
		return map;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> countByGender(MedicalSsPurchase purchase, MedicalAdmin admin) {
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		List<MedicalSsPurchase> beanList1 = new ArrayList<MedicalSsPurchase>();
		List<MedicalSsPurchase> beanList2 = new ArrayList<MedicalSsPurchase>();
		if(StringUtils.isNotBlank(purchase.getIllness())){
			beanList1 = this.iBaseDao.getBySql("from MedicalSsPurchase a where 1=1 and a.hospitalId = ? and a.sex = ? and a.illness = ? group by a.customerId order by count(a.illness) desc",
					null, admin.getEnterpriseId(), "1", purchase.getIllness());
			beanList2 = this.iBaseDao.getBySql("from MedicalSsPurchase a where 1=1 and a.hospitalId = ? and a.sex = ? and a.illness = ? group by a.customerId order by count(a.illness) desc",
					null, admin.getEnterpriseId(), "2", purchase.getIllness());
		}else{
			beanList1 = this.iBaseDao.getBySql("from MedicalSsPurchase a where 1=1 and a.hospitalId = ? and a.sex = ? group by a.customerId order by count(a.illness) desc",
					null, admin.getEnterpriseId(), "1");
			beanList2 = this.iBaseDao.getBySql("from MedicalSsPurchase a where 1=1 and a.hospitalId = ? and a.sex = ? group by a.customerId order by count(a.illness) desc",
					null, admin.getEnterpriseId(), "2");
		}
		map.put("男", beanList1.size());
		map.put("女", beanList2.size());
		return map;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object>  countByArea(MedicalSsPurchase purchase, MedicalAdmin admin) {
		Map<String, Object>  map = new LinkedHashMap<String, Object>();
		Date startTime = null;
		Date endTime = null;
		List<MedicalMediaInfo> list = this.iBaseDao.getBySql("from MedicalMediaInfo m where 1=1 and m.eid = ? group by m.cityId ", null, admin.getEnterpriseId());
		if(StringUtils.isNotBlank(purchase.getStartTime()) && StringUtils.isNotBlank(purchase.getEndTime())){
			startTime = DateUtil.getDate(purchase.getStartTime());
			endTime = DateUtil.getDate(purchase.getEndTime());
			for(MedicalMediaInfo bean : list){
				int count = this.iBaseDao.getIntCount("select count(distinct a.customerId) from MedicalSsPurchase a where 1=1 and a.hospitalId = ? and a.cityId = ? and a.ordersDate between ? and ? ", admin.getEnterpriseId(), bean.getCityId(), startTime, endTime);
				String address = bean.getAddress().substring(bean.getAddress().indexOf("-")+1, bean.getAddress().lastIndexOf("-"));
				map.put(address, count);
			}
		}else{
			for(MedicalMediaInfo bean : list){
				int count = this.iBaseDao.getIntCount("select count(distinct a.customerId) from MedicalSsPurchase a where 1=1 and a.hospitalId = ? and a.cityId = ? ", admin.getEnterpriseId(), bean.getCityId());
				String address = bean.getAddress().substring(bean.getAddress().indexOf("-")+1, bean.getAddress().lastIndexOf("-"));
				map.put(address, count);
			}
		}
		return map;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MedicalCallHealthSymptom> findCallHealthSymptomList(MedicalAdmin admin) {
		return this.iBaseDao.getBySql("from MedicalCallHealthSymptom a where a.hospitalId=? ", null, admin.getEnterpriseId());
	}
	
}
